package com.mingqi.enetconnected.view.Dialog.single;

import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.StringRes;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.date.DateInfo;
import com.mingqi.enetconnected.util.date.DateTimeInfo;
import com.mingqi.enetconnected.util.date.DateUtils;
import com.mingqi.enetconnected.util.resource.ResourceUtils;
import com.mingqi.enetconnected.view.Dialog.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

public class SinglePickerTimerEmpManager {

    private Activity mActivity;
    private Dialog mDialog;
    private TextView mTvCancel;
    private TextView mTvConfirm;
    private TextView mTvTitle;
    private ListView mLvSingle;
    private SinglePickerTimeManager.onConfirmClickListener mConfirmListener;
    private SinglePickerTimeEmpAdapter singlePickerTimeAdapter;

    public SinglePickerTimerEmpManager(Activity activity) {
        mActivity = activity;
        initDialog();
    }


    private void initDialog() {
        mDialog = new Dialog(mActivity, R.style.my_bottom_dialog);

        mDialog.setContentView(mActivity.getLayoutInflater().inflate(R.layout.dialog_single_picker, null));
        Display dd = mActivity.getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        dd.getMetrics(dm);
        WindowManager.LayoutParams attributes = mDialog.getWindow().getAttributes();
        mDialog.getWindow().setGravity(Gravity.BOTTOM);
        attributes.height = (int) (dm.heightPixels * 0.8);
        attributes.width = dm.widthPixels;

        mTvCancel = mDialog.findViewById(R.id.tvCancel);
        mTvConfirm = mDialog.findViewById(R.id.tvConfirm);
        mTvTitle = mDialog.findViewById(R.id.tvTitle);
        mLvSingle = mDialog.findViewById(R.id.lv_single);

        initData();
        setListener();

        mTvTitle.setText(ResourceUtils.getString(R.string.please_choose_single));
    }

    private void initData() {
        singlePickerTimeAdapter = new SinglePickerTimeEmpAdapter(mLvSingle, DateUtils.getMonthDateList());
        mLvSingle.setAdapter(singlePickerTimeAdapter);
        mLoadingDialog = new LoadingDialog.Builder(mActivity)
                .setMessage("加载中...")
                .setCancelable(true)
                .setCancelOutSide(true)
                .create();
    }

    protected void showProDialogHint() {

        if (mLoadingDialog.isShowing()) {
            return;
        } else {

            mLoadingDialog.show();
        }
    }

    protected void hideProDialogHint() {

        if (mLoadingDialog != null) {

            if (mLoadingDialog.isShowing()) {

                mLoadingDialog.dismiss();
            }
        }
    }


    private LoadingDialog mLoadingDialog;

    private void setListener() {

        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mConfirmListener != null) {

                    TimeValue value = getSelectValue();
                    if (value != null) {
                        dismiss();
                        mConfirmListener.onClick(value);
                    } else {
                        Toast.makeText(mActivity, ResourceUtils.getString(R.string.please_choose_right_plan_time), Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        mLvSingle.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                showProDialogHint();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        DateInfo dateInfo = (DateInfo) singlePickerTimeAdapter.getItem(position);

                        final GridViewAdapter adapter = new GridViewAdapter(db(dateInfo.getDate()));
                        dateInfo.setAdapter(adapter);
                        final GridViewAdapter empAdapter = new GridViewAdapter(dbs(dateInfo.getDate()));
                        dateInfo.setEmpAdapter(empAdapter);
                        handler.sendEmptyMessage(0);
                    }
                }).start();


            }
        });

    }

    private TimeValue getSelectValue() {
        int checkedItemPosition = mLvSingle.getCheckedItemPosition();
        if (checkedItemPosition == ListView.INVALID_POSITION) {
            return null;
        }
        DateInfo dateInfo = (DateInfo) singlePickerTimeAdapter.getItem(checkedItemPosition);
        if (dateInfo.getAdapter() == null || dateInfo.getAdapter().getSelectPosition() == ListView.INVALID_POSITION) {
            return null;
        }
        if (dateInfo.getEmpAdapter() == null || dateInfo.getEmpAdapter().getSelectPosition() == ListView.INVALID_POSITION) {
            return null;
        }
        TimeValue timeValue = new TimeValue(dateInfo.getDate() + " " + dateInfo.getAdapter().getSelectValue(), dateInfo.getValue() + " " + dateInfo.getAdapter().getSelectValue());
        timeValue.setEmpKey(dateInfo.getEmpAdapter().getSelectKey());
        timeValue.setEmpValue(dateInfo.getEmpAdapter().getSelectValue());
        return timeValue;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //  super.handleMessage(msg);
            hideProDialogHint();
            singlePickerTimeAdapter.notifyDataSetChanged();
        }
    };

    private List<DateTimeInfo> dbs(String date) {
        List<DateTimeInfo> data = new ArrayList<>();
        DateTimeInfo timeInfo = new DateTimeInfo("系统分配", false, true);
        timeInfo.setKey("");
        data.add(timeInfo);
        for (int i = 0; i < 5; i++) {
            DateTimeInfo dateTimeInfo = new DateTimeInfo("张三" + i, false, true);
            timeInfo.setKey(i + "");
            data.add(dateTimeInfo);
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return data;
    }

    private List<DateTimeInfo> db(String date) {
        List<DateTimeInfo> data = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            DateTimeInfo dateTimeInfo = new DateTimeInfo("08:00-09:0" + i, false, i != 1);
            data.add(dateTimeInfo);
        }
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return data;
    }

    public void setOnConfirmListener(SinglePickerTimeManager.onConfirmClickListener listener) {

        mConfirmListener = listener;
    }


    public interface OnCancelClickListener {

        void onClick();
    }

    public interface onConfirmClickListener {
        void onClick(TimeValue value);
    }

    public void setTitle(@StringRes int res) {
        mTvTitle.setText(res);
    }

    public void setTitle(String res) {
        mTvTitle.setText(res);
    }

    public void dismiss() {

        if (mDialog != null) {
            mDialog.cancel();
        }
    }

    public void show() {

        if (mDialog != null) {
            mDialog.show();
        }
    }
}
